import $util from "@/utils/index.js"
import $api from "@/api/index.js"
import $store from "@/store/index.js"
export default {
	state: {
		configInfo: {
			id: 0,
			isIos: uni.getSystemInfoSync().system.includes('iOS'),
			navBarHeight: uni.getSystemInfoSync().statusBarHeight * 1 + 44,
			curSysHeight: '',
			tabbarHeight: '',
			methodObj: {
				1: 'call',
				2: 'miniProgram',
				3: 'web',
				4: 'navigateTo'
			},
			tabBar: [{
				id: 1,
				name: '首页',
				default_img: 'iconshouye2',
				selected_img: 'iconshouye1'
			}, {
				id: 2,
				name: '接单池',
				default_img: 'iconqiangdan1',
				selected_img: 'iconqiangdan2'
			}, 
			{
				id: 4,
				name: '订单',
				default_img: 'icondingdan2',
				selected_img: 'icondingdan1'
			},
			{
				id: 5,
				name: '我的',
				default_img: 'iconwode1',
				selected_img: 'iconwode2'
			}],
			// primaryColor: '#6891f9'
		},
		pageArr: ['/pages/home', '/pages/pool', '/pages/personal',
			'/pages/order', '/pages/mine'
		],
		audioBg: {},
		playBg: false,
		isHaveAudio: false,
		avatarUrl: 'https://lbqnyv2.migugu.com/defaultAvatar.png', //默认头像
	},
	mutations: {
		//修改信息
		updateConfigItem(state, item) {
			let {
				key,
				val
			} = item
			state[key] = val
			if (key !== 'configInfo') return
			uni.setStorageSync('configInfo', val)
			let {
				countdown_voice
			} = val
			if (state.isHaveAudio) {
				state.audioBg.src = countdown_voice
				return
			}
			state.audioBg = uni.createInnerAudioContext();
			state.isHaveAudio = true
			state.audioBg.src = countdown_voice
			// #ifndef APP-PLUS
			state.audioBg.obeyMuteSwitch = false
			// #endif
			let play_method = [{
				method: 'onPlay',
				msg: '开始播放',
				status: true,
			}, {
				method: 'onStop',
				msg: '结束播放',
				status: false,
			}, {
				method: 'onError',
				msg: '报错Error',
				status: false,
			}, {
				method: 'onEnded',
				msg: '自然结束播放',
				status: false,
			}];
			play_method.map(item => {
				state.audioBg[item.method](() => {
					console.log('bg=>', item.msg)
					state.playBg = item.status;
				})
			})
		}
	},
	actions: {
		// 获取基本配置
		async getConfigInfo({
			commit,
			state
		}, param) {
			let [config, plugAuth] = await Promise.all([$api.base.configInfo(), $api.base.plugAuth()])
			config = Object.assign({}, config, {
				plugAuth
			})
			let {
				coachInfo
			} = $store.state.user
			let {
				is_coach = 0
			} = param ? param : {}
			if(is_coach == 1){
				coachInfo = await $api.user.coachInfo()
				$store.commit('updateUserItem', {
					key: 'coachInfo',
					val: coachInfo
				})
			}
			
			
			if (!config.primaryColor) {
				config.primaryColor = '#843CFF'
			}
			if (!config.subColor) {
				config.subColor = '#DF52FF'
			}
			if (!config.kgbt_color) {
				config.kgbt_color = '#DF52FF'
			}
			if (!config.gradualColor) {
				config.gradualColor = '#DF52FF'
			}
			if (!config.user_image) {
				config.user_image = 'https://lbqny.migugu.com/admin/anmo/mine/bg.png'
			}
			if (!config.coach_image) {
				config.coach_image = 'https://lbqny.migugu.com/admin/anmo/mine/bg.png'
			}
			if (!config.service_btn_color) {
				config.service_btn_color = '#282B34'
			}
			if (!config.service_font_color) {
				config.service_font_color = '#EBDDB1'
			}
			if (!config.user_font_color) {
				config.user_font_color = '#ffffff'
			}
			if (!config.coach_font_color) {
				config.coach_font_color = '#ffffff'
			}

			let base_config = $util.pick(state.configInfo, ['isIos', 'navBarHeight', 'curSysHeight',
				'tabbarHeight', 'methodObj', 'tabBar'
			])


			let {
				tabBar = []
			} = state.configInfo
			
			let new_tabBar = [{
				id: 1,
				name: '首页',
				default_img: 'iconshouye2',
				selected_img: 'iconshouye1'
			}, {
				id: 2,
				name: '接单池',
				default_img: 'iconqiangdan1',
				selected_img: 'iconqiangdan2'
			}, {
				id: 3,
				name: '我的团队',
				default_img: 'icontuandui',
				selected_img: 'icontuandui1'
			}, 
			{
				id: 4,
				name: '订单',
				default_img: 'icondingdan2',
				selected_img: 'icondingdan1'
			},
			{
				id: 5,
				name: '我的',
				default_img: 'iconwode1',
				selected_img: 'iconwode2'
			}]
			
			let {
				identity,
				company_id = 0
			} = coachInfo

			console.log(coachInfo, '==========> coachInfo')
			if([1,2].includes(identity) && company_id){
				tabBar = new_tabBar 
			}else{
				new_tabBar.splice(2, 1)
				tabBar = new_tabBar
			}
			
			// if (mineInd == -1) {
			// 	tabBar = [{
			// 		id: 1,
			// 		name: '首页',
			// 		default_img: 'iconshouye2',
			// 		selected_img: 'iconshouye12'
			// 	}, {
			// 		id: 2,
			// 		name: '师傅',
			// 		default_img: 'iconpeiwanguan1',
			// 		selected_img: 'iconpeiwanguan2'
			// 	}, {
			// 		id: 3,
			// 		name: '发布',
			// 		default_img: 'iconpeiwanguan1',
			// 		selected_img: 'iconpeiwanguan2'
			// 	}, 
			// 	{
			// 		id: 4,
			// 		name: '订单',
			// 		default_img: 'icondingdan3',
			// 		selected_img: 'icondingdan2'
			// 	},
			// 	{
			// 		id: 5,
			// 		name: '我的',
			// 		default_img: 'iconwode-2',
			// 		selected_img: 'iconwode-1'
			// 	}]
			// }

			// 后台diy配置顺序
			base_config.tabBar = tabBar
			let data = Object.assign({}, config, base_config)
			commit('updateConfigItem', {
				key: 'configInfo',
				val: data
			})
		},
		// 获取插件授权
		async getPlugAuth({
			commit,
			state
		}, param) {
			let plugAuth = await $api.base.plugAuth()
			let data = Object.assign({}, state.configInfo, {
				plugAuth
			})
			commit('updateConfigItem', {
				key: 'configInfo',
				val: data
			})
		},
		toPlayAudio({
			commit,
			state
		}, param) {
			if (state.playBg) {
				state.audioBg.stop()
			}
			state.audioBg.play()
		}
	}
}
